package cn.scu.ne04jdemo.service.imp;

import cn.hutool.core.util.ObjectUtil;
import cn.scu.ne04jdemo.dao.ScheDevInfoMapper;
import cn.scu.ne04jdemo.dao.ScheProdTaskInfoMapper;
import cn.scu.ne04jdemo.dao.ScheTaskDevInfoMapper;
import cn.scu.ne04jdemo.entitiy.ScheDevInfoEntity;
import cn.scu.ne04jdemo.entitiy.ScheProdTaskInfoEntity;
import cn.scu.ne04jdemo.entitiy.ScheTaskDevInfoEntity;
import cn.scu.ne04jdemo.service.ScheDevInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ScheDevInfoServiceImpl extends ServiceImpl<ScheDevInfoMapper, ScheDevInfoEntity> implements ScheDevInfoService {

    @Autowired
    private ScheDevInfoMapper scheDevInfoMapper;

    @Autowired
    private ScheProdTaskInfoMapper scheProdTaskInfoMapper;

    @Autowired
    private ScheTaskDevInfoMapper scheTaskDevInfoMapper;

    @Override
    public Boolean insert(ScheDevInfoEntity scheDevInfoEntity) {
        /**
         * 1.新增制造单元
         */
        this.scheDevInfoMapper.insert(scheDevInfoEntity);

        /**
         * 2.新增任务项与设备关联
         */
        List<ScheProdTaskInfoEntity> scheProdTaskInfoEntityList = this.scheProdTaskInfoMapper.selectList(new QueryWrapper<ScheProdTaskInfoEntity>().eq("dev_type_id", scheDevInfoEntity.getDevTypeId()));
        if (ObjectUtil.isNotNull(scheProdTaskInfoEntityList) && scheProdTaskInfoEntityList.size() > 0){
            for (ScheProdTaskInfoEntity scheProdTaskInfoEntity : scheProdTaskInfoEntityList) {
                ScheTaskDevInfoEntity scheTaskDevInfoEntity = new ScheTaskDevInfoEntity();
                scheTaskDevInfoEntity.setProdTaskId(scheProdTaskInfoEntity.getId());
                scheTaskDevInfoEntity.setDevTypeId(scheDevInfoEntity.getDevTypeId());
                scheTaskDevInfoEntity.setDevId(scheDevInfoEntity.getId());
                this.scheTaskDevInfoMapper.insert(scheTaskDevInfoEntity);
            }
        }



        return true;
    }
}
